package org.jeecg.modules.utils;

import org.jeecg.modules.bicycle.vehicle.entity.BikeVehicle;
import org.jeecg.modules.constant.BikeConstant;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

/**
 * 月月鸟
 */
@Component
public class YueYueNiaoUtils {
    //返回车桩网点加密后4位加密码
    public static String getPileCheck(String stationPileCode){
        return Objects.requireNonNull(MD5Utils.getMD5String(stationPileCode)).substring(0,4);
    }
    //返回年龄
    public static int getAgeByIdCard(String idcardNumber) {
        int age = 0;

        if (idcardNumber == null || "".equals(idcardNumber)) {
            return age;
        }

        if (idcardNumber.length() != 15 && idcardNumber.length() != 18) {
            return age;
        }
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);

        int year = Integer.valueOf(idcardNumber.substring(6, 10));
        int month = Integer.valueOf(idcardNumber.substring(10, 12));
        int day = Integer.valueOf(idcardNumber.substring(12, 14));

        if ((month < monthNow) || (month == monthNow && day <= dayNow)) {
            age = yearNow - year;
        } else {
            age = yearNow - year - 1;
        }
        return age;
    }
    //判断是否成年  成年返回1
    public static String idcardNumberToAge(String idcardNumber) {
        if (idcardNumber == null || "".equals(idcardNumber)) {
            return "身份证件号有误,无法计算年龄";
        }

        if (idcardNumber.length() != 15 && idcardNumber.length() != 18) {
            return "身份证件号有误,无法计算年龄";
        }
        int age = 0;


        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);

        int year = Integer.valueOf(idcardNumber.substring(6, 10));
        int month = Integer.valueOf(idcardNumber.substring(10, 12));
        int day = Integer.valueOf(idcardNumber.substring(12, 14));

        if ((month < monthNow) || (month == monthNow && day <= dayNow)) {
            age = yearNow - year;
        } else {
            age = yearNow - year - 1;
        }

        if (age<=12){
            return "用户未成年";
        }
        if (age>=100){
            return "用户超过100岁，无法借车";
        }
        return BikeConstant.y_n_yes;
    }
    //数组去重
    public static String[] onlyArrayClear(String[] arrStr) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < arrStr.length; i++) {
            if (!list.contains(arrStr[i])) {
                list.add(arrStr[i]);
            }
        }
        //返回一个包含所有对象的指定类型的数组
        String[] newArrStr =  list.toArray(new String[1]);
        return newArrStr;
    }
    //数组转数组格式String
    public static String arrayToString (String[] array){
        String s = Arrays.toString(array);
        return s.replace("[","").replace("]","").replace(" ","");
    }

    //List BikeVehicle集合 去除指定id 和 前面的内容
    public static List<BikeVehicle> deleteFrontDataById(List<BikeVehicle> list , String id ){
        //获取要删除的字符
        List<String> delIndex = new ArrayList<>();
        for (int i  = 0 ; i<list.size() ; i++){
            BikeVehicle vehicle = list.get(i);

            delIndex.add(i + "");
            //判断匹配
            if (vehicle.getId().equals(id)) {
                //删除之前下标
                for (int r = delIndex.size() - 1; r >= 0; r--) {
                    list.remove(Integer.parseInt(delIndex.get(r)));
                }
                break;
            }
        }
        return list;
    }
    //纯数字UUID
    public static String  getUUid() {
        UUID uuid = UUID.randomUUID();
        String uuid_old = uuid.toString();
        System.out.println(uuid_old);
        String uuid_new = (uuid_old.substring(0, 8)
                + uuid_old.substring(9, 13) +
                uuid_old.substring(14, 18)
                + uuid_old.substring(19, 23)
                + uuid_old.substring(24, 36)).toUpperCase();
        return uuid_new;
    }
    //当天结束时间
    public static Date getToDayEnd(){
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        ZonedDateTime zonedDateTime = today_end.atZone(zoneId);
        return Date.from(zonedDateTime.toInstant());
    }
    //当天开始时间
    public static Date getToDayStart(){
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        ZonedDateTime zonedDateTime = today_start.atZone(zoneId);
        return Date.from(zonedDateTime.toInstant());
    }


}
